package com.gzy.blog.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gzy.blog.blog.dao.ArticleDao;
import com.gzy.blog.blog.dao.BlogDao;
import com.gzy.blog.blog.dao.CategoryDao;
import com.gzy.blog.blog.dao.DiscussDao;
import com.gzy.blog.blog.entity.Article;
import com.gzy.blog.blog.entity.Blog;
import com.gzy.blog.blog.entity.Category;
import com.gzy.blog.blog.entity.Discuss;
import com.gzy.blog.blog.service.BlogService;
import com.gzy.blog.common.PageModel;
import com.gzy.blog.sys.dao.LogDao;

@Service
public class BlogServiceImpl implements BlogService {

	@Autowired
	private BlogDao blogDao;
	@Autowired
	private LogDao logDao;
	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private DiscussDao discussDao;
	@Autowired
	private CategoryDao categoryDao;

	/**
	 * 新增一个用户，并记录日志
	 */
	@Override
	// @Transactional
	// @Transactional(propagation = Propagation.REQUIRED)
	public void add(Blog blog) {
		blogDao.addBlog(blog);
		logDao.addLog();
	}

	/**
	 * <pre>
	 * &#64;Transactional的配置
	 * propagation  默认为required，也是用得最多的
	 * readOnly     默认为false，设置为true表示只做查询（可提高查询效率）
	 * </pre>
	 */
	@Override
	// @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Blog get(Integer id) {
		return blogDao.get(id);
	}

	@Override
	public PageModel<Blog> listBlogPage(Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		return blogDao.listBlogPage(pageNo, pageSize);
	}

	@Override
	public void delete(Blog blog) {
		// TODO Auto-generated method stub
		blogDao.delete(blog);
	}

	@Override
	public void update(Blog blog) {
		// TODO Auto-generated method stub
		blogDao.update(blog);
	}

	@Override
	public PageModel<Blog> listBlogWithUser(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return blogDao.listBlogWithUser(pageNo, pageSize);
	}

	@Override
	public PageModel<Article> pageArticle(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return articleDao.pageArticle(pageNo, pageSize);
	}

	@Override
	public Blog getBlogByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return blogDao.getBlogByUserId(userId);
	}

	@Override
	public PageModel<Article> getArticlePageByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return articleDao.getArticlePageByUserId(userId);
	}

	@Override
	public List<Article> listWellAticle() {
		// TODO Auto-generated method stub
		return articleDao.listWellArticle();
	}

	@Override
	public Blog getBlogByArticleId(Integer id) {
		// TODO Auto-generated method stub
		return blogDao.getBlogByArticleId(id);
	}

	@Override
	public Article getArticleById(Integer id) {
		// TODO Auto-generated method stub
		return articleDao.getArticleById(id);
	}

	@Override
	public List<Discuss> getDiscussListByArticleId(Integer id) {
		// TODO Auto-generated method stub
		return discussDao.getDiscussListByArticleId(id);
	}

	@Override
	public Blog getBlogById(Integer id) {
		// TODO Auto-generated method stub
		return blogDao.getBlogById(id);
	}

	@Override
	public List<Article> listWellAticleSelf(Integer userId) {
		// TODO Auto-generated method stub
		return articleDao.listWellAticleSelf(userId);
	}

	@Override
	public void deleteDiscussById(Integer id) {
		// TODO Auto-generated method stub
		discussDao.delete(id);
	}

	@Override
	public PageModel<Discuss> pageDiscuss(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return discussDao.pageDiscuss(pageNo, pageSize);
	}

	@Override
	public void addDiscuss(Discuss discuss) {
		// TODO Auto-generated method stub
		discussDao.add(discuss);
	}

	@Override
	public List<Category> listCategorySelf(Integer userId) {
		// TODO Auto-generated method stub
		return categoryDao.listCategorySelf(userId);
	}

	@Override
	public List<Article> listArticleCategory(Integer userId) {
		// TODO Auto-generated method stub
		return articleDao.listArticleCategory(userId);
	}

	@Override
	public PageModel<Category> pageCategory(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return categoryDao.pageCategory(pageNo, pageSize);
	}

	@Override
	public int getSun(List<Article> categoryList) {
		// TODO Auto-generated method stub
		int sum = 0;
		for (Article article : categoryList) {
			sum += article.getCategorySum();
		}
		return sum;
	}

	@Override
	public Article getSingleArticle(Integer id) {
		// TODO Auto-generated method stub
		return articleDao.getArticleById(id);
	}

	@Override
	public void addCategory(Category category) {
		// TODO Auto-generated method stub
		categoryDao.add(category);
	}

	@Override
	public void addArticle(Article article) {
		// TODO Auto-generated method stub
		articleDao.add(article);
	}

	@Override
	public void deleteArticle(Integer id) {
		// TODO Auto-generated method stub
		articleDao.delete(id);
	}

	@Override
	public void deleteCategory(Integer id) {
		// TODO Auto-generated method stub
		categoryDao.delete(id);
	}

}
